package com.example.homework8;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.homework8.bean.MoneyInfo;
import com.example.homework8.bean.PlanInfo;
import com.example.homework8.bean.RecordInfo;
import com.example.homework8.bean.UserInfo;
import com.example.homework8.database.MoneyDBHelper;
import com.example.homework8.database.PlanDBHelper;
import com.example.homework8.database.RecordDBHelper;
import com.example.homework8.database.UserHelper;
import com.example.homework8.utils.SharedUtil;

import java.util.ArrayList;


public class LoginActivity extends AppCompatActivity {
    UserHelper helper;
    private EditText user, password;
    private Button login, register;
    private String name;
    private String psd;
    private boolean door=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        user = (EditText) findViewById(R.id.edt1);
        password = (EditText) findViewById(R.id.edt2);
        login = (Button) findViewById(R.id.btn1);
        register = (Button) findViewById(R.id.btn2);
        //数据库操作
        requestReadExternalPermission();
        requestWriteExternalPermission();
    }
    @SuppressLint("NewApi")
    private void requestReadExternalPermission() {
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
        ) {
            Log.d("TAG", "READ permission IS NOT granted...");
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE ,Manifest.permission.CAMERA}, 0);

        } else {
            Log.d("TAG", "READ permission is granted...");
        }
    }

    @SuppressLint("NewApi")
    private void requestWriteExternalPermission() {
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            Log.d("TAG", "WRITE permission IS NOT granted...");
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);

        } else {
            Log.d("TAG", "WRITE permission is granted...");
        }
    }
    public void click1(View view) {
        //获取输入框文本
        String classname = "";
        String phone = "";
        String imgurl="";
        String username = user.getText().toString().trim();
        String mypassword = password.getText().toString().trim();
        helper = new UserHelper(this, "SAVE_USER1.db", null, 1);
        SQLiteDatabase database = helper.getReadableDatabase();
        Cursor cursor = database.rawQuery("select * from user", null);
        while(cursor.moveToNext()){
            //从数据库获取到用户名、密码
            name = cursor.getString(cursor.getColumnIndex("name"));
            psd = cursor.getString(cursor.getColumnIndex("password"));
            phone = cursor.getString(cursor.getColumnIndex("phone"));
            imgurl = cursor.getString(cursor.getColumnIndex("imgurl"));
            classname = cursor.getString(cursor.getColumnIndex("classname"));

            if(name.equals(username) && psd.equals(mypassword)){
                door=true;
                break;
            }
        }cursor.close();
        if (username.equals("") && mypassword.equals("")) {
            Toast.makeText( LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            if (door) {
                //账号密码校验正确
                // 登陆成功，跳转的用户界面
                SharedUtil.getIntance(this).writeShared("loginuser",username);
                SharedUtil.getIntance(this).writeShared("classname",classname);
                SharedUtil.getIntance(this).writeShared("phone",phone);
                SharedUtil.getIntance(this).writeShared("imgurl",imgurl);
                Log.d("TAG", "click1: imimgurlgurl "+imgurl);

                ArrayList<RecordInfo> recordInfos =   RecordDBHelper.getInstance(LoginActivity.this,1).query("username = " +username);
                ArrayList<PlanInfo> planInfos = PlanDBHelper.getInstance(LoginActivity.this,1).query("username = " +username);
                ArrayList<MoneyInfo> moneyInfos = MoneyDBHelper.getInstance(LoginActivity.this,1).query("username =  " +username );

                if(recordInfos.size() <= 0 ){
                    ArrayList<RecordInfo> infos = RecordInfo.getDefaultList(this,username);
                    for ( RecordInfo recordInfo: infos) {
                        RecordDBHelper.getInstance(LoginActivity.this,1).insert(recordInfo);
                    }
                }
                if(planInfos.size() <= 0 ){
                    ArrayList<PlanInfo> infos = PlanInfo.getDefaultList(username);
                    for ( PlanInfo recordInfo: infos) {
                        PlanDBHelper.getInstance(LoginActivity.this,1).insert(recordInfo);
                    }
                }
                if(moneyInfos.size() <= 0 ){
                    Log.d("TAG", "click1:moneyInfos size is 0 ");
                    ArrayList<MoneyInfo> infos = MoneyInfo.getDefaultList(username);
                    for ( MoneyInfo recordInfo: infos) {
                        MoneyDBHelper.getInstance(LoginActivity.this,1).insert(recordInfo);
                    }
                }
                Toast.makeText( LoginActivity.this, "登陆成功！", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("name",username);
                startActivity(intent);
                //提示
            } else {
                Toast.makeText(LoginActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void click2(View view) {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);
    }
}
